package com.hanter.android.radwidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;

import com.etoonet.android.radwidget.R;

public class TitleBar extends RelativeLayout {

    private View compatPrimaryDark;
    private ViewGroup compatTitleBar;
    private ViewGroup titleBar;
    private TextView title;
    private TextView leftBarButton;
    public LinearLayout leftBar;
    private TextView rightBarButton;
    public LinearLayout rightBar;
    private View titleBarLine;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View.inflate(context, R.layout.view_cupertino_title_bar, this);

        compatPrimaryDark = findViewById(R.id.compatPrimaryDark);
        title = findViewById(R.id.title);
        compatTitleBar = this;
        titleBar = findViewById(R.id.titleBar);
        titleBarLine = findViewById(R.id.titleBarLine);
        leftBarButton = findViewById(R.id.leftBarButton);
        leftBar = findViewById(R.id.leftBar);
        rightBarButton = findViewById(R.id.rightBarButton);
        rightBar = findViewById(R.id.rightBar);

        initAttrs(context, attrs, defStyleAttr);
    }

    private void initAttrs(Context context, AttributeSet attrs, @StyleRes int styleRes) {
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.TitleBar,
                R.attr.titleBarStyle,
                styleRes == 0 ? R.style.TitleBar : styleRes);

        String titleText = t.getString(R.styleable.TitleBar_title);
        String leftButtonText = t.getString(R.styleable.TitleBar_leftBarButtonText);
        String rightButtonText = t.getString(R.styleable.TitleBar_rightBarButtonText);
        int lineColor = t.getColor(R.styleable.TitleBar_titleBarLineColor, 0xFFE8E8E8);
        int visibility = t.getBoolean(R.styleable.TitleBar_titleBarLineVisible, false) ? View.VISIBLE : View.GONE;

        int colorPrimary = t.getColor(R.styleable.TitleBar_colorPrimary, Color.TRANSPARENT);
        int colorPrimaryDark = t.getColor(R.styleable.TitleBar_colorPrimaryDark, Color.TRANSPARENT);
        int titleTextColor = t.getColor(R.styleable.TitleBar_titleBarTextColor, 0xFF3B3D47);
        int barButtonTextColor = t.getColor(R.styleable.TitleBar_titleBarButtonTextColor, 0xFF3B3D47);

        title.setText(titleText);
        title.setTextColor(titleTextColor);
        leftBarButton.setText(leftButtonText);
        leftBarButton.setTextColor(barButtonTextColor);
        rightBarButton.setText(rightButtonText);
        rightBarButton.setTextColor(barButtonTextColor);
        titleBarLine.setBackgroundColor(lineColor);
        titleBarLine.setVisibility(visibility);

        compatTitleBar.setBackgroundColor(colorPrimary);
        compatPrimaryDark.setBackgroundColor(colorPrimaryDark);

        t.recycle();
    }


    public void setTitle(CharSequence title) {
        this.title.setText(title);
    }

    public void setTitle(int resId) {
        this.title.setText(resId);
    }

    public void setSubtitle(CharSequence subtitle) {

    }

    public void setSubtitle(int resId) {

    }

    @Nullable
    public CharSequence getTitle() {
        return title.getText();
    }

}
